from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()


class Author(db.Model):
    a_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    a_name = db.Column(db.String(32))
    a_detail = db.Column(db.Text)


class Dynasty(db.Model):
    d_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    d_name = db.Column(db.String(32))


class Collection(db.Model):
    c_id = db.Column(db.Integer, primary_key=True, autoincrement=True)


class Opinion(db.Model):
    o_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    o_content = db.Column(db.Text)

    o_user_id = db.Column(db.Integer, db.ForeignKey('user.u_id'))


class Type(db.Model):
    t_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    t_name = db.Column(db.String(32))


class Remark(db.Model):
    r_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    r_content = db.Column(db.Text)

    r_user_id = db.Column(db.Integer, db.ForeignKey('user.u_id'))


class User(db.Model):
    u_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    u_name = db.Column(db.String(64))
    u_password = db.Column(db.String(64))


class Poem(db.Model):
    p_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    p_name = db.Column(db.String(64))
    p_content = db.Column(db.Text)
    p_analyze = db.Column(db.Text)
    p_annotation = db.Column(db.Text)
    p_view = db.Column(db.Integer, default=0)
    p_collect_count = db.Column(db.Integer, default=0)

    p_author_id = db.Column(db.Integer, db.ForeignKey('author.a_id'))
    # p_type_id = db.Column(db.Integer, db.ForeignKey('type.t_id'))
    p_dynasty_id = db.Column(db.Integer, db.ForeignKey('dynasty.d_id'))
    p_remark_id = db.Column(db.Integer, db.ForeignKey('remark.r_id'))

    collects = db.relationship('Collection', secondary='poem_collection',backref=db.backref('poem', lazy='dynamic'), lazy='dynamic')
    types = db.relationship('Type', secondary='poem_type',backref=db.backref('poem', lazy='dynamic'), lazy='dynamic')


poem_type = db.Table('poem_type',
    db.Column('poem_id', db.Integer, db.ForeignKey('poem.p_id')),
    db.Column('type_id', db.Integer, db.ForeignKey('type.t_id'))
)

poem_collection = db.Table('poem_collection',
    db.Column('poem_id', db.Integer, db.ForeignKey('poem.p_id')),
    db.Column('collection_id', db.Integer, db.ForeignKey('collection.c_id'))
)